package com.neusoft.controller;



import java.util.ArrayList;
import java.util.List;

import com.neusoft.feign.FoodFeignClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.neusoft.po.Business;
import com.neusoft.po.CommonResult;
import com.neusoft.service.BusinessService;


//@CrossOrigin("*")

import javax.annotation.Resource;

@RestController
@RequestMapping("/BusinessController")
@RefreshScope //开启动态刷新
public class BusinessController {

	@Autowired
	private BusinessService businessService;
	@Resource
	private FoodFeignClient foodFeignClient;
	//很重要  没有跑不起来：在com.neusoft.controller.BusinessController中注入了一个RestTemplate类型的bean,但是在配置类中没有定义这个bean。
	  @Bean
	  public RestTemplate restTemplate() {
	    return new RestTemplate();
	  }
	
	@GetMapping("/listBusinessByOrderTypeId/{orderTypeId}")
	public CommonResult<List> listBusinessByOrderTypeId(@PathVariable("orderTypeId") Integer orderTypeId)
			throws Exception {
		List<Business> list = businessService.listBusinessByOrderTypeId(orderTypeId);
		return new CommonResult(200, "success", list);
	}

	@GetMapping("/getBusinessById/{businessId}")
   public CommonResult<Business> getBusinessById(@PathVariable("businessId") Integer businessId) throws Exception{

		Business business = businessService.getBusinessById(businessId);
		//在商家微服务中调用食品微服务
		CommonResult<List> result = foodFeignClient.listFoodByBusinessId(businessId);
		//如果食品微服务返回降级响应，那么就返回空集合
		if (result.getCode() == 200) {
			business.setFoodList(result.getResult());
		}else {
			business.setFoodList(new ArrayList());
		}

		return new CommonResult(200,"success",business);
   }

	// 直接获取所有商家信息
	@GetMapping("/businesses")
	public CommonResult listBusiness(Business business) throws Exception{
		List<Business> list = businessService.listBusiness();
		return new CommonResult(200,"success",list);
	}
}
